package dviz.visualSystem.impl;

import java.util.Map;

public class ExpInterpolate implements ValueInterpolate {
	float alpha;
	Class valueType;

	double lastValue;

	public ExpInterpolate() {
		alpha = 0.1f;
	}

	@Override
	public void addValue(Object value) {
		if (valueType == null) {
			valueType = value.getClass();
			if (valueType == Float.class) {
				lastValue = (Float) value;
			} else if (valueType == Double.class) {
				lastValue = (Double) value;
			} else if (valueType == Integer.class) {
				lastValue = (Integer) value;
			} else if (valueType == Character.class) {
				lastValue = (Character) value;
			} else
				throw new RuntimeException("Cannot interpolate type:"
						+ valueType.getName());
		} else {
			if (valueType == Float.class) {
				lastValue = lastValue * (1f - alpha) + alpha * (Float) value;
			} else if (valueType == Double.class) {
				lastValue = lastValue * (1f - alpha) + alpha * (Double) value;
			} else if (valueType == Integer.class) {
				lastValue = (int) (lastValue * (1f - alpha) + alpha
						* (Integer) value);
			} else if (valueType == Character.class) {
				lastValue = (char) (lastValue * (1f - alpha) + alpha
						* (Character) value);
			} else
				throw new RuntimeException("Cannot interpolate type:"
						+ valueType.getName());
		}
	}

	@Override
	public Object getValue() {
		if (valueType == Float.class) {
			return Float.valueOf((float) lastValue);
		} else if (valueType == Double.class) {
			return Double.valueOf((double) lastValue);
		} else if (valueType == Integer.class) {
			return Integer.valueOf((int) lastValue);
		} else if (valueType == Character.class) {
			return Character.valueOf((char) lastValue);
		} else
			throw new RuntimeException("Cannot interpolate type:"
					+ valueType.getName());
	}

	@Override
	public void setProperty(Map<String, String> property) {
		if (property.get("alpha") != null)
			alpha = new Float(property.get("alpha"));
	}

	@Override
	public void update(float fpt) {

	}

}
